<?php
namespace common\models\goods;

use Yii;
use yii\db\ActiveRecord;
//use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

class Attr extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%attribute}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['store_id', 'attr_name','goods_commonid'], 'required'],
            [['member_id','store_id','attr_show','attr_sort','goods_commonid'], 'integer'],
            [['attr_name'], 'string', 'max' => 40],
            [['attr_value'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'attr_id' => 'ID',
            'member_id' => '用户ID',
            'store_id' => '店铺ID',
            'goods_commonid'=>'商品公共ID',
            'attr_name' => '属性名',
            'attr_value'=>'属性值',
            'attr_show' => '是否显示',
            'attr_sort' => '排序',
        ];
    }

    public function getValues(){
        return $this->hasMany(AttrValue::className(),['attr_id'=>'attr_id'])
            ->select(['attr_value_id','attr_value_name','attr_value_sort','attr_id','is_show'])
            ->where(['is_show'=>1]);
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'create' => [''],
//            'backendCreate' => ['username', 'password_hash'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }

    /**
     * @return array
     */
//    public function behaviors()
//    {
//        return [
//            [
//                'class' => TimestampBehavior::class,
//                'attributes' => [
//                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
//                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
//                ],
//            ],
//        ];
//    }
}
